package cn.wh.xwm.hibernate.dao;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.criterion.Example;

import cn.wh.xwm.common.util.TimeUtils;
import cn.wh.xwm.controller.action.vo.report.custinout.Application;
import cn.wh.xwm.controller.action.vo.report.custinout.Categories;
import cn.wh.xwm.controller.action.vo.report.custinout.Category;
import cn.wh.xwm.controller.action.vo.report.custinout.CategoryA;
import cn.wh.xwm.controller.action.vo.report.custinout.Chart;
import cn.wh.xwm.controller.action.vo.report.custinout.CustomerInAndOutJson;
import cn.wh.xwm.controller.action.vo.report.custinout.Dataset;
import cn.wh.xwm.controller.action.vo.report.custinout.Definition;
import cn.wh.xwm.controller.action.vo.report.custinout.Styles;
import cn.wh.xwm.controller.action.vo.report.sales.Data;
import cn.wh.xwm.controller.action.vo.report.sales.SalesJson;

/**
 * A data access object (DAO) providing persistence and search support for
 * SalesLiquid entities. Transaction control of the save(), update() and
 * delete() operations can directly support Spring container-managed
 * transactions or they can be augmented to handle user-managed Spring
 * transactions. Each of these methods provides additional information for how
 * to configure it for the desired type of transaction control.
 * 
 * @see cn.wh.xwm.hibernate.dao.SalesLiquid
 * @author MyEclipse Persistence Tools
 */

public class SalesLiquidDAO extends BaseHibernateDAO {
	private static final Log log = LogFactory.getLog(SalesLiquidDAO.class);
	// property constants
	public static final String IN_OR_OUT = "inOrOut";
	public static final String AMOUNT = "amount";
	public static final String PATCH_NUMBER = "patchNumber";
	public static final String USER_ID = "userId";

	public void save(SalesLiquid transientInstance) {
		log.debug("saving SalesLiquid instance");
		try {
			getSession().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	public void delete(SalesLiquid persistentInstance) {
		log.debug("deleting SalesLiquid instance");
		try {
			getSession().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public SalesLiquid findById(java.lang.String id) {
		log.debug("getting SalesLiquid instance with id: " + id);
		try {
			SalesLiquid instance = (SalesLiquid) getSession().get(
					"cn.wh.xwm.hibernate.dao.SalesLiquid", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List findByExample(SalesLiquid instance) {
		log.debug("finding SalesLiquid instance by example");
		try {
			List results = getSession().createCriteria(
					"cn.wh.xwm.hibernate.dao.SalesLiquid").add(
					Example.create(instance)).list();
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public List findByProperty(String propertyName, Object value) {
		log.debug("finding SalesLiquid instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from SalesLiquid as model where model."
					+ propertyName + "= ?";
			Query queryObject = getSession().createQuery(queryString);
			queryObject.setParameter(0, value);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List findByInOrOut(Object inOrOut) {
		return findByProperty(IN_OR_OUT, inOrOut);
	}

	public List findByAmount(Object amount) {
		return findByProperty(AMOUNT, amount);
	}

	public List findByPatchNumber(Object patchNumber) {
		return findByProperty(PATCH_NUMBER, patchNumber);
	}

	public List findByUserId(Object userId) {
		return findByProperty(USER_ID, userId);
	}

	public List findAll() {
		log.debug("finding all SalesLiquid instances");
		try {
			String queryString = "from SalesLiquid";
			Query queryObject = getSession().createQuery(queryString);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public SalesLiquid merge(SalesLiquid detachedInstance) {
		log.debug("merging SalesLiquid instance");
		try {
			SalesLiquid result = (SalesLiquid) getSession().merge(
					detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(SalesLiquid instance) {
		log.debug("attaching dirty SalesLiquid instance");
		try {
			getSession().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(SalesLiquid instance) {
		log.debug("attaching clean SalesLiquid instance");
		try {
			getSession().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void update(SalesLiquid persistentInstance) {
		log.debug("update SalesLiquid instance");
		try {
			getSession().update(persistentInstance);
			log.debug("update successful");
		} catch (RuntimeException re) {
			log.error("update failed", re);
			throw re;
		}

	}

	public SalesJson findForReport(String customerNameId, String salesId,
			String startDate, String endDate) {
		UserinfoDAO udao = new UserinfoDAO();
		Userinfo user = udao.findById(salesId);
		String title = user.getUsername()+"销售报表";
		SalesJson json = prepare(title);

		ProductinfoDAO pdao = new ProductinfoDAO();
		List<Productinfo> plist = pdao.findAll();
		for (Productinfo p : plist) {
			Data data = new Data();
			data.setLabel(p.getName());
			data.setValue(this.findData(salesId, customerNameId, startDate,
					endDate, p.getId()));
			json.setData(data);
		}
		return json;
	}

	private String findData(String salesId, String customerNameId,
			String startDate, String endDate, String productId) {
		String sql = "select sum(amount) from snfims.sales_liquid where user_id='"+salesId+"' and product_id ='"
				+ productId
				+ "' and event_date >'"
				+ startDate
				+ "' and event_date <'" + endDate + "' ";
		if(customerNameId!=null&&!customerNameId.trim().equals("")){
			sql=sql+" and customer_id_to ='"+customerNameId+"'";
		}

		return this.findSum(sql);
	}

	private SalesJson prepare(String title) {
		SalesJson json = new SalesJson();
		Chart chart = new Chart();
		chart.setCaption(title);
		chart.setNumberprefix("支");
		chart.setShowvalues("1");
		chart.setXaxisname("产品");
		chart.setYaxisname("销量");
		json.setChart(chart);
		Styles styles = new Styles();
		Definition definition = new Definition();
		definition.setDuration("1");
		definition.setName("CanvasAnim");
		definition.setParam("_xScale");
		definition.setStart("0");
		definition.setType("animation");
		ArrayList<Definition> delist = new ArrayList<Definition>();
		delist.add(definition);
		styles.setDefinition(delist);
		ArrayList<Application> applist = new ArrayList<Application>();
		Application app = new Application();
		applist.add(app);
		app.setStyles("CanvasAnim");
		app.setToobject("Canvas");
		styles.setApplication(applist);
		json.setStyles(styles);
		return json;
	}

	
	
}